package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsProductVelocityMapper;
import com.jy.wms.pojo.WmsGoodsBalance;
import com.jy.wms.pojo.WmsProductVelocity;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;



/**
 * 产品周转率报表
 * Created by syh on 2018/5/8.
 */
@Service
public class WmsProductVelocityService extends BaseService<WmsProductVelocityMapper,WmsProductVelocity> {
    /**
     * 分页查询
     * @param entity
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "产品周转率报表",btnName = "分页查询产品周转率报表")
    public Page<WmsProductVelocity> baseSelectPage(WmsProductVelocity entity) throws ServiceException {
        try{
            if(entity!=null && !("").equals(entity.getNytime()) && entity.getNytime()!=null){
                getFalgTime(entity);
            }else{
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                c.add(Calendar.MONTH, -1);
                Date m = c.getTime();
                String mon = format.format(m);
                entity.setNytime(mon);
            }
            PageHelper.startPage(entity.getPage(),entity.getRows());
            return (Page<WmsProductVelocity>) dao.baseSelectPage(entity);
        }catch(Exception e){
            throw  new ServiceException("查询产品周转率报表失败",e);
        }
    }
    /**
     * 分页查询和导出提取 改时间格式的共同部分
     * @param entity
     * @throws ParseException
     */
    public void getFalgTime(WmsProductVelocity entity) throws ParseException {
        if (entity!=null && !("").equals(entity.getNytime()) && entity.getNytime()!=null){
            String date =entity.getNytime();//年月
            date = date.replace("Z", " UTC");//注意是空格+UTC
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
            Date d = format2.parse(date);
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM");//注意格式化的表达式
            entity.setNytime(format1.format(d).toString());
        }
    }

    /**
     * 导出查询
     * @param entity
     * @return
     */
    public List<WmsProductVelocity> getVelocityExport(WmsProductVelocity entity) throws ServiceException{
        try{
            getFalgTime(entity);
            return  dao.baseSelectPage(entity);
        }catch(Exception e){
            throw  new ServiceException("导出查询产品周转率报表失败",e);
        }
    }

    /**
     * 产品周转率报表 定时器查询信息
     * @throws Exception
     */
    @BaseServiceDescribe(moduleName = "产品周转率报表 定时器查询信息", btnName = "产品周转率报表信息")
    @Transactional
    public void getVelocityTimer() throws Exception{
        JSONObject json=getTime();
        //第一个方法:查所有产品种类
        List<WmsProductVelocity> list=dao.getVelocity();
        for(WmsProductVelocity wmsProductVelocity : list){
            int yckzs=0;
            int yckxs=0;
            int yckts=0;
            //查支数,用产品类型id
            List<WmsProductVelocity> listZs= dao.getCkZs(wmsProductVelocity.getCplxid(), json.get("fysj_start").toString(),json.get("fysj_end2").toString());
            for(WmsProductVelocity zs:listZs){
                yckzs+=zs.getYckzs();
                //查箱数,用货品id(月出库箱数(传入第一天的开始和最后一天的结束时间,产品类型id))
                List<WmsProductVelocity> listXs=dao.getCkxs(zs.getHpid(), zs.getYckzs());
                for(WmsProductVelocity xs:listXs){
                    yckxs+=xs.getYckxs();
                }
                //查托数,计算月出库托数{月出库支数/(TI*HI*箱主单位数量)
                List<WmsProductVelocity> listTs=dao.getCkts(zs.getHpid(), zs.getYckzs());
                for(WmsProductVelocity ts:listTs){
                    yckts+=ts.getYckts();
                }
            }
            //出库支数
            wmsProductVelocity.setYckzs(yckzs);
            //出库箱数
            wmsProductVelocity.setYckxs(yckxs);
            //出库托数
            wmsProductVelocity.setYckts(yckts);
            wmsProductVelocity.setId(UUIDUtils.uuid());
            wmsProductVelocity.setNytime(json.get("nytime").toString());
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, -1);
            //第四个:期初数量,期末数量
            WmsGoodsBalance gb=dao.selectQc(sdf1.format(calendar.getTime()),wmsProductVelocity.getCplxid());
            //月平均库存支数(期末数量+期初数量)/2
            Integer ypjkczs=(Integer.parseInt(gb.getSyjc())+Integer.parseInt(gb.getByjc()))/2;
            wmsProductVelocity.setYpjkczs(ypjkczs);
            //计算库存周转率kczzl
            if( ypjkczs!=null && !ypjkczs.equals(0) && !"".equals(ypjkczs)){
                DecimalFormat df = new DecimalFormat("0.00");//格式化小数
                String s = df.format(wmsProductVelocity.getYckzs());//返回的是String类型
                Float kczzl1= Float.valueOf(s)/ypjkczs/100;
                java.math.BigDecimal kczzl = new java.math.BigDecimal(kczzl1);
                BigDecimal b=new BigDecimal(0);
                if(kczzl.equals(b)){
                    wmsProductVelocity.setKczzl(b);
                }
                wmsProductVelocity.setKczzl(kczzl);
            }else {
                BigDecimal b = new BigDecimal(0);
                wmsProductVelocity.setKczzl(b);
            }
            //第六个:储位周转率cwzzl(月出库托数/该品类规划储位)
            Integer cwsl=dao.getGhcw(wmsProductVelocity.getCplxid());
            if(cwsl!=null && !cwsl.equals(0) && !"".equals(cwsl)){
                DecimalFormat df = new DecimalFormat("0.00");//格式化小数
                String s = df.format(yckts);//返回的是String类型
                Float cwzzl2= Float.valueOf(s)/cwsl/100;
                java.math.BigDecimal cwzzl = new java.math.BigDecimal(cwzzl2);
                BigDecimal b=new BigDecimal(0);
                if(cwzzl.equals(b)){
                    wmsProductVelocity.setCwzzl(b);
                }
                wmsProductVelocity.setCwzzl(cwzzl);
            }else  {
                BigDecimal b = new BigDecimal(0);
                wmsProductVelocity.setCwzzl(b);
            }
            dao.baseInsert(wmsProductVelocity);
        }
    }

    public JSONObject getTime() {
        JSONObject json = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        //获取前一个月的月日
        String nytime = sdf1.format(calendar.getTime());
        json.put("nytime",nytime);
        //获取前一个月第一天
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, -1);
        calendar1.set(Calendar.DAY_OF_MONTH,1);
        calendar1.set(Calendar.HOUR_OF_DAY, 00);
        calendar1.set(Calendar.MINUTE, 00);
        calendar1.set(Calendar.SECOND, 00);
        //fysj_start上间个月第一天的开始时
        String fysj_start = sdf.format(calendar1.getTime());
        json.put("fysj_start",fysj_start);
        calendar1.set(Calendar.HOUR_OF_DAY, 23);
        calendar1.set(Calendar.MINUTE, 59);
        calendar1.set(Calendar.SECOND, 59);
        //fysj_end上个月第一天的结束时间
        String fysj_end = sdf.format(calendar1.getTime());
        json.put("fysj_end",fysj_end);
        //获取前一个月最后一天
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.DAY_OF_MONTH, 0);
        calendar2.set(Calendar.HOUR_OF_DAY, 00);
        calendar2.set(Calendar.MINUTE, 00);
        calendar2.set(Calendar.SECOND, 00);
        //fysj_start2上个月最后一天的开始时间
        String fysj_start2 = sdf.format(calendar2.getTime());
        json.put("fysj_start2",fysj_start2);
        calendar2.set(Calendar.HOUR_OF_DAY, 23);
        calendar2.set(Calendar.MINUTE, 59);
        calendar2.set(Calendar.SECOND, 59);
        //fysj_end2上个月最后一天的结束时间
        String fysj_end2 = sdf.format(calendar2.getTime());
        json.put("fysj_end2",fysj_end2);
        return json;
    }

}